<?php
/**
 * Implement command WNavigator/addPage
 * <pre>
 * Arguments:
 * 1. name (string, mandatory)
 *    Page name for URL
 * 2. meta (array, optional)
 *    Meta values, such as title, keywords, description, name in menu and etc.
 * 3. meta_uri (string, optional)
 *    URI of the meta values source
 * 4. content (string, mandatory)
 *    URI of the page layout
 * 5. path (string, mandatory)
 *    Path to the parent page 
 * 6. save (boolean, optional) 
 *    Save updated map to file of not. Default value "false".
 * Result:
 * Data structure   
 * page
 * |-name
 * |-meta [uri]
 * | |-<value>+
 * |-content
 * |-children
 * | |-page+
 * </pre>
 * @package WNavigator
 */
require_once 'WKernel/Command/Abstract.php';
class WN_Cmd_addPage extends WK_Command_Abstract {
	public function __construct(){
		parent::__construct();
		$this->params['save'] = false;
	}
	public function exec(){
		$nav  = WK_Application::getComponent('WNavigator');
		$page = $nav->newPage($this->params['name']);
		if(empty($this->params['meta']['title']))
			$this->params['meta']['title'] = $this->params['meta']['name'];
		$page->setMeta(@$this->params['meta_uri'], $this->params['meta']);
		$page->setContent($this->params['content']);
		$parent = $nav->getMap()->getPage($this->params['path']);
		if(!empty($parent)) {
			$parent->addChild($page);
			if($this->params['save'])	
				$nav->saveMap();
			$this->result = $page->toData();
		}
		else {
			throw WK_Application::getException(404, 'Page not found!');
		}
	}	
}